# coding: utf-8
# Author: zhenda

import re


def is_mobile(mobile_str):
    """
    验证手机号格式
    :param mobile_str:
    :return:
    """
    if re.match(r'^1[3-9]\d{9}$', mobile_str):
        return mobile_str
    else:
        raise ValueError(f'{mobile_str} is not a valid mobile')


def is_email(email_str):
    """
    验证邮箱格式
    :param email_str:
    :return:
    """
    if re.match(r'^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$', email_str):
        return email_str
    else:
        raise ValueError(f'{email_str} is not a valid email')


def regex(pattern):
    """
    正则校验
    :param pattern: str 正则表达式
    :return:
    """
    def validate(value_str):
        """
        校验字符串
        :return:
        """
        if re.match(pattern, value_str):
            return value_str
        else:
            raise ValueError(f'{value_str} is not valid of {pattern}')
    return validate

